<?php
namespace App\Exception\Handler;

use App\Exception\LimitedException;
use Swoft\Error\Annotation\Mapping\ExceptionHandler;
use Swoft\Http\Message\ContentType;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Exception\Handler\AbstractHttpErrorHandler;
use Throwable;

/**
 * Class LimitedExceptionHandler
 * @package App\Exception\Handler
 * @ExceptionHandler(LimitedException::class)
 */
class LimitedExceptionHandler extends AbstractHttpErrorHandler
{

    /**
     * @param Throwable $e
     * @param Response $response
     * @return Response
     */
    public function handle(Throwable $e, Response $response): Response
    {
        $data = [
            'status'=>400,
            'error_code'=>444,
            'error_user_title'=>'服务访问限制',
            'error_user_msg'=>'目前访问量过大请稍后重试!',
            'message'=>"访问被限流",
        ];
        return $response->withHeader('Access-Control-Allow-Origin', '*')
            ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
            ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS')
            ->withContentType(ContentType::JSON)->withData($data);
    }
}
